/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  const stack = [] //新建栈
  const map = new Map() //新建map字典
  // 定义map字典
  map.set('(', ')')
  map.set('[', ']')
  map.set('{', '}')
  for (let i = 0; i < s.length; i++) { //循环遍历
    if (map.has(s[i])) { //如果有左边
      stack.push(s[i]) //入栈
    } else { //不是左边
      let top = stack[stack.length - 1] //获取栈顶元素
      if (map.get(top) === s[i]) stack.pop() //如果元素匹配字典栈顶，出栈
      else return false //否则false
    }
  }
  return stack.length === 0 //防止只剩下左边 false/true
};